﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Thu Vien Them Vao
//Thu Vien Them Vao
using CrystalDecisions.Shared;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;
using System.Globalization;
//using CrystalDecisions.CrystalReports.Engine;
//using CrystalDecisions.Shared;


namespace QuanLyQuanCafe
{
    public partial class FrmTraCuuHoaDon : Form
    {
        HoaDonBL hoadonBL = new HoaDonBL();
        CT_HoaDonBL ct_hoadonBL = new CT_HoaDonBL();
        string maHD = "";
        public FrmTraCuuHoaDon()
        {
            InitializeComponent();
        }

        private void FrmTraCuuHoaDon_Load(object sender, EventArgs e)
        {
            dataGridViewHoaDon.DataSource = hoadonBL.LayDanhSachTraCuuHoaDon();

            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }
            panelChiTietHoaDon.Visible = false;
        }

        private void btnHuyTraCuu_Click(object sender, EventArgs e)
        {
            txtMaBan.Text = "";
            txtMaHoaDon.Text = "";
            txtNgay.Text = "";
            txtThang.Text = "";
            txtNam.Text = "";
        }        

        private void txtMaHoaDon_TextChanged(object sender, EventArgs e)
        {
            string maHD = txtMaHoaDon.Text;
            string maBan = txtMaBan.Text;
            string thang = txtThang.Text;
            string ngay = txtNgay.Text;
            string nam = txtNam.Text;

            dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDon(maHD, maBan, ngay, thang, nam);
            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }

            //string maHD = txtMaHoaDon.Text;
            //string maBan = txtMaBan.Text;                        

            //dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDonBinhThuong(maHD, maBan);
            //for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            //{
            //    dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            //}
        }

        private void txtMaBan_TextChanged(object sender, EventArgs e)
        {
            string maHD = txtMaHoaDon.Text;
            string maBan = txtMaBan.Text;
            string thang = txtThang.Text;
            string ngay = txtNgay.Text;
            string nam = txtNam.Text;

            dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDon(maHD, maBan, ngay, thang, nam);
            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }
            //string maHD = txtMaHoaDon.Text;
            //string maBan = txtMaBan.Text;

            //dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDonBinhThuong(maHD, maBan);
            //for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            //{
            //    dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            //}
        }

        private void txtNgay_TextChanged(object sender, EventArgs e)
        {
            string maHD = txtMaHoaDon.Text;
            string maBan = txtMaBan.Text;
            string thang = txtThang.Text;
            string ngay = txtNgay.Text;
            string nam = txtNam.Text;

            dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDon(maHD, maBan, ngay, thang, nam);
            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }
            //string maHD = txtMaHoaDon.Text;
            //string maBan = txtMaBan.Text;
            //string ngay = txtNgay.Text;


            //dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDonTheoNgay(ngay, 1);
            //for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            //{
            //    dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            //}
        }

        private void txtThang_TextChanged(object sender, EventArgs e)
        {

            string maHD = txtMaHoaDon.Text;
            string maBan = txtMaBan.Text;
            string thang = txtThang.Text;
            string ngay = txtNgay.Text;
            string nam = txtNam.Text;

            dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDon(maHD, maBan, ngay, thang, nam);
            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }//string maHD = txtMaHoaDon.Text;
            //string maBan = txtMaBan.Text;
            //string ngay = txtThang.Text;


            //dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDonTheoNgay(ngay, 2);
            //for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            //{
            //    dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            //}
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            string maHD = txtMaHoaDon.Text;
            string maBan = txtMaBan.Text;
            string thang = txtThang.Text;
            string ngay = txtNgay.Text;
            string nam = txtNam.Text;

            dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDon(maHD, maBan, ngay, thang, nam);
            for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            }
            //string maHD = txtMaHoaDon.Text;
            //string maBan = txtMaBan.Text;
            //string ngay = txtNam.Text;


            //dataGridViewHoaDon.DataSource = hoadonBL.LayKetQuaDanhSachTraCuuHoaDonTheoNgay(ngay, 3);
            //for (int i = 0; i < dataGridViewHoaDon.Rows.Count - 1; i++)
            //{
            //    dataGridViewHoaDon.Rows[i].Cells["colSTT"].Value = i + 1;
            //}
        }

        private void dataGridViewHoaDon_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            maHD = dataGridViewHoaDon.Rows[row].Cells["MaHD"].Value.ToString();
        }

        private void btnXemChiTietHoaDon_Click(object sender, EventArgs e)
        {
            lbChiTietHoaDon.Text = "Chi Tiết Hóa Đơn: " + maHD;
            dataGridViewChiTietHoaDon.DataSource = ct_hoadonBL.LayDanhSachTraCuuChiTietHoaDon(maHD);
            for (int i = 0; i < dataGridViewChiTietHoaDon.Rows.Count - 1; i++)
            {
                dataGridViewChiTietHoaDon.Rows[i].Cells["colSTT_CT"].Value = i + 1;
            }

            panelChiTietHoaDon.Visible = true;
        }

        private void btnDongLai_Click(object sender, EventArgs e)
        {
            panelChiTietHoaDon.Visible = false;            
        }

        private void btnInLaiHoaDon_Click(object sender, EventArgs e)
        {
            InHoaDon inhoadon = new InHoaDon();
            ParameterValues a = new ParameterValues();
            ParameterDiscreteValue b = new ParameterDiscreteValue();

            DataTable datatable = ct_hoadonBL.LayDanhSachTraCuuChiTietHoaDon(maHD);
            DataTable _datatable = hoadonBL.DSHoaDon(maHD);

            inhoadon.SetDataSource(datatable);

            b.Value = "";
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["KhachHang"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["MaHD"].ToString();
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["MaHD"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["MaBan"].ToString();
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["MaBan"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["Ngay"].ToString();
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["Ngay"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["KhuyenMai"].ToString();
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["KhuyenMai"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["DenBu"].ToString();
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["DenBu"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["PhiPhucVu"].ToString() + " Đồng";
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["PhiPhucVu"].ApplyCurrentValues(a);

            b.Value = _datatable.Rows[0]["TongTien"].ToString() + " Đồng";
            a.Add(b);
            inhoadon.DataDefinition.ParameterFields["TongTien"].ApplyCurrentValues(a);

            FrmInHoaDon frminhoadon = new FrmInHoaDon(inhoadon);
            frminhoadon.ShowDialog();
            frminhoadon.Dispose();
        }
    }
}
